import Group from "./Group.js";
function createGroup(id, x, y, width, height, fill, stroke) {
  const group = new Group({
    id,
    x,
    y,
    width,
    height,
    attrs: {
      body: { fill, stroke },
      label: { text: id },
    },
    isGroup: true,
  });
  return group;
}
function createNode(id, x, y, width, height, fill) {
  return {
    id,
    x,
    y,
    width,
    height,
    shape: "circle",
    attrs: {
      body: {
        fill: fill || "white",
        stroke: "black",
      },
      label: {
        text: id,
        fill: "#fff",
        fontSize: 12,
      },
    },
  };
}
function createEdge(id, source, target, vertices) {
  return {
    id,
    source,
    target,
    label: {
      attrs: {
        label: {
          text: id.split("->")[1],
          textVerticalAnchor: "middle",
          textAnchor: "top",
        },
      },
      position: {
        distance: 0.1,
        offset: -10,
      },
    },
    vertices,
  };
  4;
}
function getPosition(graph, id) {
  return graph.getCellById(id).position();
}
const upPosition = { x: 1, y: 1 };
function drawGraph(graph, tableData) {
  tableData.map((item) => {
    const { id, children, datastart, dataend, y } = item;
    const start = graph.getCellById(datastart).position();
    const end = graph.getCellById(dataend).position();
    if (!children || children.length === 0) {
      graph.addNode(
        createNode(
          id,
          start.x,
          // graph.getCellById(item.parent).position().y + 40 * upPosition.y++,
          y,
          20,
          20
        )
      );
    } else {
      graph.addNode(
        createGroup(
          id,
          start.x - 40,
          // start.y + 40 * upPosition.y++,
          y,
          end.x - start.x,
          900 - 40 * upPosition.y++,
          " rgba(255, 255, 255, 0.488)",
          "black"
        )
      );
      drawGraph(graph, children);
    }
  });
}
function addParent(graph, data) {
  data.map((item) => {
    if (item.parent && item.id > 85 && item.id < 101) {
      graph.getCellById(item.parent).addChild(graph.getCellById(item.id));
    }
  });
}
function addLines(graph, data) {
  console.log(data);
  data.map((item) => {
    const { id, source, target } = item;
    console.log(graph.getCellById(source));
    if (id >= 77 && id <= 91) {
      const vertices = [
        {
          x: getPosition(graph, target).x,
          y: getPosition(graph, source).y + 10,
        },
      ];
      graph.addEdge(
        createEdge(`${source}->${target}`, source, target)
      );
    }
  });
}
export { createGroup, createNode, createEdge, drawGraph, addParent, addLines };
